// pages/user/user.js
const config = require('../../config')
const util = require('../../utils/util')
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabbar_active:1,
        filePath: 'http://duodianzhufu-1253586992.cosgz.myqcloud.com/Resources/',

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            filePath: config.filePath + 'Resources/'
        })
        
    },
    


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getinfo()
    },
    //保存formid
    saveform: function (e) {
        var formid = e.detail.formId
        app.saveformid(formid)
    }, 
    //获取信息
    getinfo: function () {
        var that = this
        if (app.globalData.uid) {
            if (app.globalData.nickName){
                that.setData({
                    nickName: app.globalData.nickName,
                    avatarUrl: app.globalData.avatarUrl
                })
            }
            var headerkey = util.createheaderkey()
            
            wx.request({
                url: config.host + '/api/getwish/myinfo',
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                    'key': headerkey.key,
                    'time': headerkey.timestamp
                },
                data: {
                    uid: app.globalData.uid
                },
                success: function (res) {
                    if (res.data.code == 200) {
                        var data = res.data.data
                        that.setData({
                            balance: data.balance.balance,
                            sendgiftData: data.sendgiftData,
                            sendwishData: data.sendwishData
                        })
                    } else {
                        wx.showModal({
                            title: '失败',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                },
                fail: function (res) {
                    wx.showToast({
                        title: '用户信息获取失败!',
                        duration: 3000
                    })
                }
            })
        } else {
            setTimeout(function () {
                that.getinfo()
            }, 1000)
        }
    },
    getuserinfo:function(){
        app.getuserinfo()
    },
    //修改tabbar
    changetabbar:function(){
        var tabbar_active
        this.data.tabbar_active == 1 ? tabbar_active=2 :tabbar_active=1
        this.setData({
            tabbar_active:tabbar_active
        })
    },
    goto:function(e){
        var page = e.currentTarget.dataset.page
        wx.navigateTo({
            url: '/pages/' + page
        })
    },
})